package com.tekj.cxqcb.view.Module.activity.StoreModule;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.ApiShopServiceTypeBean;
import com.tekj.cxqcb.operation.resultBean.ShopGoodsListBean;
import com.tekj.cxqcb.view.Module.adapter.NewStoreDetailsAdapter;
import com.tekj.cxqcb.view.Module.adapter.StoreTypeAdapter;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;

/***
 * 店铺商品管理
 */
public class CommodityManagementActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.rv_classification)
    RecyclerView rvClassification;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;

    BusinessDao businessDao;
    StoreTypeAdapter storeTypeAdapter;
    NewStoreDetailsAdapter storeDetailsAdapter;

    @Override
    public void init() {
        tvTitle.setText("店铺商品管理");
        tvR.setText("新增");
        tvR.setTextColor(getResources().getColor(R.color.colorPrimary));

        businessDao = new BusinessDao(activity, this);
        businessDao.ApiShopServiceType(MainBActivity.User.getShopId(), "");

        storeTypeAdapter = new StoreTypeAdapter(null);
        rvClassification.setLayoutManager(new LinearLayoutManager(activity));
        rvClassification.setAdapter(storeTypeAdapter);
//        rvClassification.performClick();
        storeTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                storeTypeAdapter.setSelection(position);
                businessDao.ShopGoodsList(storeTypeAdapter.getData().get(position).getId(), MainBActivity.User.getShopId());
            }
        });

        storeDetailsAdapter = new NewStoreDetailsAdapter(null);
        rvMain.setLayoutManager(new LinearLayoutManager(activity));
        rvMain.setAdapter(storeDetailsAdapter);

        storeDetailsAdapter.setEmptyView(R.layout.rv_empty, rvMain);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_commodity_management;
    }

    boolean isFirst;

    @Override
    public void initLoadStart() {
        if (isFirst) {
            loading.show();
            isFirst = false;
        } else
            showDialog();
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 查询所有分类:
                ApiShopServiceTypeBean typeBean = (ApiShopServiceTypeBean) bindingViewBean.getBean();
                if (typeBean.getCode().equals("0000000")) {
                    if (typeBean.getData() != null && typeBean.getData().size() > 0) {
                        typeBean.getData().get(0).setSelection(true);
                        businessDao.ShopGoodsList(typeBean.getData().get(0).getId(), MainBActivity.User.getShopId());
                    }
                    storeTypeAdapter.setNewData(typeBean.getData());

                }

                break;
            case 店铺商品:
                ShopGoodsListBean goodsListBean = (ShopGoodsListBean) bindingViewBean.getBean();

                if (goodsListBean.getCode().equals("0000000")) {

                    storeDetailsAdapter.setNewData(goodsListBean.getData().getList());
                }
                break;
        }
    }


    @OnClick({R.id.ll_back, R.id.ll_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_right:
                startActivity(new Intent(activity,AddStoreActivity.class));
                break;
        }
    }
}
